package com.cgsd.heyu.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.library.utils.ToastHelper;
import com.cgsd.heyu.R;
import com.cgsd.heyu.ui.info.CashOutActivity;

/**
 * @author xiaoxing
 * Description 提现账号提示
 * CreateData: 2020/11/3
 */
public class InputCashOutDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private TextView tvCancel,tvConfirm;
    private DialogListener dialogListener;
    private TextView tvTitle;
    private TextView mTvName;
    private EditText mEdtName;
    private TextView mTvAccount;
    private EditText mEdtAccount;

    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }

    public InputCashOutDialog(@NonNull Context context) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        setContentView(R.layout.dialog_input_cash_out);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
        initView();
    }

    private void initView() {
        mTvName = findViewById(R.id.tv_name);
        mEdtName = findViewById(R.id.edt_name);
        mTvAccount = findViewById(R.id.tv_account);
        mEdtAccount = findViewById(R.id.edt_account);
        tvTitle = findViewById(R.id.tv_title);
        tvCancel = findViewById(R.id.tv_cancel);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                if (dialogListener != null){
                    dialogListener.cancel();
                }
                break;
            case R.id.tv_confirm:
                confirm();
                break;
            default:
        }
    }

    /**
     * 点击确认
     */
    private void confirm(){
        String name = mEdtName.getText().toString();
        String account = mEdtAccount.getText().toString();
        if (name.length() <= 0){
            ToastHelper.showDefaultToast("请输入用户名");
            return;
        }
        if (account.length() <= 0){
            ToastHelper.showDefaultToast("请输入账号");
            return;
        }
        if (dialogListener != null){
            dialogListener.confirm(name,account);
        }
    }

    /**
     * 设置类型
     * @param cashOutType
     */
    public void setType(String cashOutType){
        mEdtName.setText("");
        mEdtAccount.setText("");
        if (CashOutActivity.CASH_OUT_WEIXIN.equals(cashOutType)){
            tvTitle.setText("微信提现");
            mTvName.setText("微信用户名");
            mEdtName.setHint("请输入你的微信用户名");
            mTvAccount.setText("微信提现账号");
            mEdtAccount.setHint("请输入你的微信账号");
        }else {
            tvTitle.setText("支付宝提现");
            mTvName.setText("支付宝真实姓名");
            mEdtName.setHint("请输入你的支付宝真实姓名");
            mTvAccount.setText("支付宝提现账号");
            mEdtAccount.setHint("请输入你的支付宝账号");
        }
    }


    public interface DialogListener{
        /**
         * 确认
         * @param account 账号
         * @param name 用户名
         */
        void confirm(String name,String account);

        /**
         * 取消
         */
        void cancel();
    }
}
